/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.courier.view;

import com.sun.courier.dal.CourierDAL;
import com.sun.courier.entity.Courier;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HONG QUAN
 */
public class CourierActions extends JFrame implements ActionListener {

    CourierDAL courierDal = new CourierDAL();
    
    JPanel pnMain = new JPanel();
    JPanel pnTitle = new JPanel();
    JPanel pnCenter = new JPanel();
    JPanel pnButton = new JPanel();

    JLabel lblTitle = new JLabel();
    JLabel lblCourierId = new JLabel("Courier Id");
    JLabel lblCourierName = new JLabel("Courier Name");
    JLabel lblCustomerName = new JLabel("Customer Name");
    JLabel lblPhone = new JLabel("Phone");
    JLabel lblStatus = new JLabel("Status");
    JLabel lblWeight = new JLabel("Weight");
    JLabel lblDepartLocation = new JLabel("Departed Location");
    JLabel lblDestination = new JLabel("Destination");
    JLabel lblDistance = new JLabel("Distance");

    JTextField txtCourierId = new JTextField();
    JTextField txtCourierName = new JTextField();
    JTextField txtCustomerName = new JTextField();
    JTextField txtPhone = new JTextField();
    JTextField txtStatus = new JTextField();
    JTextField txtWeight = new JTextField();
    JTextField txtDepartLocation = new JTextField();
    JTextField txtDestination = new JTextField();
    JTextField txtDistance = new JTextField();

    JButton btnAdd = new JButton("Insert");
    JButton btnUpdate = new JButton("Update");
    JButton btnReset = new JButton("Reset");

    DefaultTableModel model = null;
    int row = -1;
    
    public CourierActions(DefaultTableModel model, int row) throws Exception {
        registerListeners();
        this.model = model;
        this.row = row;
        if(row == -1){
            int lastId = 1; //courierDal.getLastId().get(0).getCourierId();
            lastId = lastId +1;
            txtCourierId.setText(Integer.toString(lastId));
        }
        createControlPanel();
    }

    public void createControlPanel(){
        pnTitle.setLayout(new FlowLayout());
        pnTitle.setBorder(BorderFactory.createEtchedBorder());
        lblTitle.setFont(new Font("Arial", Font.BOLD, 25));
        pnTitle.add(lblTitle);

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        pnCenter.setBorder(BorderFactory.createEtchedBorder());
        pnCenter.setLayout(gridbag);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 3, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        pnCenter.add(lblCourierId, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 0, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        txtCourierId.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtCourierId, c);

        c.gridx = 0;
        c.gridy = 2;
        pnCenter.add(lblCourierName, c);

        c.gridx = 1;
        c.gridy = 2;
        txtCourierName.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtCourierName, c);

        c.gridx = 0;
        c.gridy = 3;
        pnCenter.add(lblCustomerName, c);

        c.gridx = 1;
        c.gridy = 3;
        txtCustomerName.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtCustomerName, c);

        c.gridx = 0;
        c.gridy = 4;
        pnCenter.add(lblPhone, c);

        c.gridx = 1;
        c.gridy = 4;
        txtPhone.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtPhone, c);

        c.gridx = 0;
        c.gridy = 5;
        pnCenter.add(lblStatus, c);

        c.gridx = 1;
        c.gridy = 5;
        txtStatus.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtStatus, c);

        c.gridx = 0;
        c.gridy = 6;
        pnCenter.add(lblWeight, c);

        c.gridx = 1;
        c.gridy = 6;
        txtWeight.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtWeight, c);

        c.gridx = 0;
        c.gridy = 7;
        pnCenter.add(lblDepartLocation, c);

        c.gridx = 1;
        c.gridy = 7;
        txtDepartLocation.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtDepartLocation, c);

        c.gridx = 0;
        c.gridy = 8;
        pnCenter.add(lblDestination, c);

        c.gridx = 1;
        c.gridy = 8;
        txtDestination.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtDestination, c);

        c.gridx = 0;
        c.gridy = 9;
        pnCenter.add(lblDistance, c);

        c.gridx = 1;
        c.gridy = 9;
        txtDistance.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtDistance, c);

        pnButton.setLayout(new FlowLayout());
        pnButton.setBorder(BorderFactory.createEtchedBorder());
        if(row == -1){
            pnButton.add(btnAdd);
            lblTitle.setText("Add New Courier");
        }else{
            pnButton.add(btnUpdate);
            lblTitle.setText("Update Courier");
        }
        pnButton.add(btnReset);

        pnMain.setLayout(new BorderLayout());
        pnMain.setBackground(Color.LIGHT_GRAY);
        pnMain.setBorder(BorderFactory.createRaisedSoftBevelBorder());
        pnMain.add(pnTitle, BorderLayout.NORTH);
        pnMain.add(pnCenter, BorderLayout.CENTER);
        pnMain.add(pnButton, BorderLayout.SOUTH);

        getContentPane().add(pnMain, BorderLayout.CENTER);
    }
    
    public void setValues(ArrayList<Courier> lst) {

        txtCourierId.setText(Integer.toString(lst.get(0).getCourierId()));
        txtCourierName.setText(lst.get(0).getCourierName());
        txtCustomerName.setText(lst.get(0).getCustomerName());
        txtPhone.setText(lst.get(0).getPhone());
        txtStatus.setText(lst.get(0).getStatus());
        txtWeight.setText(lst.get(0).getWeight());
        txtDepartLocation.setText(lst.get(0).getDepartLocation());
        txtDestination.setText(lst.get(0).getDestination());
        txtDistance.setText(lst.get(0).getDistance());
    }

    protected void updateCourier() {
        Courier c = new Courier();
        c.setCourierId(Integer.parseInt(txtCourierId.getText()));
        c.setCourierName(txtCourierName.getText());
        c.setCustomerName(txtCustomerName.getText());
        c.setPhone(txtPhone.getText());
        c.setStatus(txtStatus.getText());
        c.setWeight(txtWeight.getText());
        c.setDepartLocation(txtDepartLocation.getText());
        c.setDestination(txtDestination.getText());
        c.setDistance(txtDistance.getText());
        Boolean rs = courierDal.updateCourier(c);
        if (rs) {
            JOptionPane.showMessageDialog(pnCenter, "Update Successful!", "Result", JOptionPane.INFORMATION_MESSAGE);
            model.setValueAt(c.getCourierId(), row, 0);
            model.setValueAt(c.getCourierName(), row, 1);
            model.setValueAt(c.getCustomerName(), row, 2);
            model.setValueAt(c.getPhone(), row, 3);
            model.setValueAt(c.getStatus(), row, 4);
            model.setValueAt(c.getWeight(), row, 5);
            model.setValueAt(c.getDepartLocation(), row, 6);
            model.setValueAt(c.getDestination(), row, 7);
            model.setValueAt(c.getDistance(), row, 8);
            model.fireTableDataChanged();
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(pnCenter, "Update Fail!", "Result", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    private void registerListeners() {
        btnUpdate.addActionListener(this);
        btnAdd.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        try {
            if (ae.getSource().equals(btnUpdate)) {
                updateCourier();
            }else if(ae.getSource().equals(btnAdd)){
                addNewCourier();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void addNewCourier() {
        Courier c = new Courier();
        c.setCourierName(txtCourierName.getText());
        c.setCustomerName(txtCustomerName.getText());
        c.setPhone(txtPhone.getText());
        c.setStatus(txtStatus.getText());
        c.setWeight(txtWeight.getText());
        c.setDepartLocation(txtDepartLocation.getText());
        c.setDestination(txtDestination.getText());
        c.setDistance(txtDistance.getText());
        Boolean rs = courierDal.insertCourier(c);
        Object[][] data = null;
        if (rs) {
            JOptionPane.showMessageDialog(pnCenter, "Insert Successful!", "Result", JOptionPane.INFORMATION_MESSAGE);
            data = new Object[1][10];
            data[0][0] = txtCourierId.getText();
            data[0][1] = txtCourierName.getText();
            data[0][2] = txtCustomerName.getText();
            data[0][3] = txtPhone.getText();
            data[0][4] = txtStatus.getText();
            data[0][5] = txtWeight.getText();
            data[0][6] = txtDepartLocation.getText();
            data[0][7] = txtDestination.getText();
            data[0][8] = txtDistance.getText();
            model.addRow(data[0]);
            model.fireTableDataChanged();
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(pnCenter, "Insert Fail!", "Result", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}
